package com.zzz.framework.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class MyJsonUtil {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static <T> T toBean(String json, Class<T> clz) {
        if (StringUtils.isBlank(json)) {
            return null;
        }
        try {
            return mapper.readValue(json, clz);
        } catch (Exception e) {
            log.error("toBean", e);
        }
        return null;
    }

    public static String toJsonStr(Object bean) {
        if (bean == null) {
            return null;
        }
        try {
            return mapper.writeValueAsString(bean);
        } catch (Exception e) {
            log.error("toJsonStr", e);
        }
        return null;
    }
}
